# -*- coding:utf-8 -*-
import socket
# 获取目标主机ip和port
target_host = 'www.google.com'
target_port = 80
# 创建tcp套接字
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立tcp链接
client.connect_ex((target_host, target_port))
# 发送消息
send_msg = 'GET / HTTP/1.1\r\nHost:google.com\r\n\r\n'
client.send(send_msg.encode('utf-8'))
# 接收消息
recv_msg = client.recv(4096)
# 关闭套接字
client.close()
print(recv_msg)
